﻿using System;
using System.Collections.ObjectModel;

namespace CoreLib
{
    public class SortedObservableCollection<T> : ObservableCollection<T>
    {
        private Comparison<T> _comparison;

        public SortedObservableCollection( Comparison<T> comparison )
        {
            _comparison = comparison;
        }

        public new void Add( T item )
        {
            int insertLocation = -1;

            for( int i = 0; i < Items.Count; i++ )
            {
                var compareResult = _comparison( item, Items[i] );

                if( compareResult < 0 )
                    continue;

                insertLocation = i;
                break;
            }

            if( insertLocation == -1 )
                base.Add( item );
            else
                base.InsertItem( insertLocation, item );
        }
    }
}
